﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web.UI.HtmlControls;
using System.Xml;
using Quizz.Infrastructure;
using Quizz.Library.QuizzServiceReference;

namespace Quizz.Library.Helper
{
    public sealed class MenuHelper
    {
        public static string GenerateMainMenu(int activeId)
        {
            using (var proxy = new QuizzProjectServiceClient())
            {
                var isLoggedIn = AccessFactory.IsUserLogined();
                IList<Menu> menus = isLoggedIn
                                  ? proxy.MenuGetAllForRole(AccessFactory.CurrentUser.RoleId)
                                  : proxy.MenuGetForAnonymous();

                var xmlDoc = new XmlDocument();
                var menu = BuildContent(menus, xmlDoc, activeId);

                return menu.OuterXml;
            }
        }


        private static XmlElement BuildContent(IList<Menu> menus, XmlDocument xmlDoc, long activeId = -1)
        {
            var parents = menus.Where(m => m.ParentMenuId == null).ToList();
            activeId = activeId == -1 ? (int)parents.First().Id : activeId;
            var result = xmlDoc.CreateElement("ul");
            result.SetAttribute("id", "main-menu");
            foreach (var item in parents)
            {
                var li = xmlDoc.CreateElement("li");
                if (ParentId(item) == activeId)
                    li.SetAttribute("class", "current");
                var a = xmlDoc.CreateElement("a");
                var url = item.Type == (int)MenuType.Dynamic
                    ? string.Format(@"/Page/View/{0}", item.Id)
                    : string.IsNullOrEmpty(item.Url)
                        ? "javascript:void(0)"
                        : item.Url;
                a.SetAttribute("href", url);
                a.InnerText = item.Name;
                li.AppendChild(a);

                // Build child
                var id = item.Id;
                var childs = menus.Where(m => m.ParentMenuId == id).ToList();
                if (childs.Count() > 0)
                    li.AppendChild(BuildChild(childs, xmlDoc));

                result.AppendChild(li);
            }
            return result;
        }

        private static XmlElement BuildChild(IList<Menu> menus, XmlDocument xmlDoc)
        {
            var result = xmlDoc.CreateElement("ul");
            result.SetAttribute("class", "sub-menu");

            foreach (var item in menus)
            {
                var li = xmlDoc.CreateElement("li");
                var a = xmlDoc.CreateElement("a");
                var url = item.Type == (int)MenuType.Dynamic
                    ? string.Format(@"/Page/View/{0}", item.Id)
                    : string.IsNullOrEmpty(item.Url)
                        ? "javascript:void(0)"
                        : item.Url;
                a.SetAttribute("href", url);
                a.InnerText = item.Name;

                li.AppendChild(a);
                result.AppendChild(li);
            }

            return result;
        }

        private static long ParentId(Menu current)
        {
            if (current.ParentMenuId != null)
            {
                return current.ParentMenuId.Value;
            }
            return current.Id;
        }
    }
}
